package com.guigu.qingcheng.servicegoods.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.guigu.qingcheng.pojo.goods.TbCategory;
import com.guigu.qingcheng.servicegoods.service.TbCategoryService;
import com.guigu.qingcheng.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 商品类目 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2020-07-08
 */
@RestController
@RequestMapping("/category")
public class TbCategoryController {

    @Autowired
    TbCategoryService categoryService;

    //查询所有
    @GetMapping("findAll")
    public R findAll(){
        List<TbCategory> list = this.categoryService.list(null);
        if (list.size()>0)
            return R.ok().data("list",list);
        return R.error().data("list",list);
    }

    //分页不带条件
    @GetMapping("findPage")
    public R findPage(@RequestParam("page") int page, @RequestParam("size") @PathVariable int size){
        //创建page对象
        Page<TbCategory> pageTbCategory = new Page<>(page,size);
        this.categoryService.page(pageTbCategory,null);

        long total = pageTbCategory.getTotal();//总记录数
        List<TbCategory> records = pageTbCategory.getRecords(); //数据list集合
        if (total>0 && records.size()>0)
            return R.ok().data("total",total).data("rows",records);
        return R.error();
    }

    //条件查询不带分页
    @PostMapping("findList")
    public R findList(@RequestBody Map<String,Object> searchMap){
        List<TbCategory> list = categoryService.findList(searchMap);
        if (list.size()>0){
            return R.ok().data("list",list);
        }
        return R.error().data("list",list);
    }

    //条件查询带分页
    @PostMapping("findPage")
    public R pageTeacherCondition(@RequestBody Map<String, Object> searchMap,@RequestParam("page") int page, @RequestParam("size") int size){
        Page<TbCategory> pageCategory = categoryService.findPage(searchMap,page,size);
        if (pageCategory.getRecords().size()>0){
            return R.ok().data("total",pageCategory.getTotal()).data("rows",pageCategory.getRecords());
        }
        return R.error().data("total",pageCategory.getTotal()).data("rows",pageCategory.getRecords());
    }

    //根据id查询
    @GetMapping("findById")
    public R findById(@RequestParam("id") int id){
        TbCategory category = new TbCategory();
        category = categoryService.getById(id);
        return R.ok().data("category",category);
    }

    //根据id查询
    @GetMapping("findById2")
    public TbCategory findById2(@RequestParam("id") int id){
        TbCategory category = new TbCategory();
        category = categoryService.getById(id);
        return category;
    }


    //增加数据
    @PostMapping("add")
    public R addCategory(@RequestBody TbCategory tbCategory){
        Boolean flag = categoryService.add(tbCategory);
        if (flag)
            return R.ok();
        return R.error();
    }
    //修改数据
    @PostMapping("update")
    public R updateCateGory(@RequestBody TbCategory tbCategory){
        Boolean flag = this.categoryService.update(tbCategory);
        if (flag)
            return R.ok();
        return R.error();
    }

    //删除数据
    @GetMapping("delete/{id}")
    public R deleteCateGoryById(@PathVariable int id){
        QueryWrapper<TbCategory> wrapper = new QueryWrapper<>();
        wrapper.eq("parent_id",id);
        List<TbCategory> list = categoryService.list(wrapper);
        if (list.size() > 0){
            return R.error();
        }else{
            boolean flag = categoryService.removeById(id);
            if (flag)
                return R.ok();
            return R.error();
        }
    }

    @GetMapping("/findCategoryList")
    public List<Map> findCategoryList(){
      List<Map> mapList =  categoryService.findCategoryTree();
      return mapList;
    }
}

